var filePath = global.app.filePath;
var child_process = require('child_process');
var spawn = child_process.spawn;

var mimeTypes = {
    'JPEG':'jpg',
    'PNG':'png',
    'GIF':'gif'
};

module.exports = function (fileName, callback) {
    var path = filePath + fileName;
    var identify = spawn('identify', ['-format', '%b,%w,%h,%m', path]);
    var _str = '';
    identify.stdout.on('data', function (data) {
        _str += data.toString();
    });
    identify.on('exit', function (code) {
        if (code === null) {
            callback({
                'error':1,
                'message':_str.toString()
            });
            return;
        }
        var str = _str.replace(/\n/g, '');
        var WH = str.split(',');
        var type = mimeTypes[WH[3]];
        var mv = spawn('mv', [path, path + '.' + type]);
        mv.on('exit', function () {
            callback({
                'error':0,
                'data':{
                    size:Math.ceil(WH[0].replace('B', '') / 1024 * 1000) / 1000 + 'KB',
                    width:WH[1],
                    height:WH[2],
                    type:type,
                    fileName:fileName + '.' + type
                }
            });
        });
    });
};